// packageI/taskPackage/record/record.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeClass: "",
    type: "",
    url: "",
    sub_team_count: "",
    show: false,
    reward_id: "",
    rankingList: [],
    rankingList: null,
    otherJson: {},

    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.type) {
      this.setData({
        type: options.type
      })
    }
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
    this.init();
    if (options.type == 1) {
      // 奖励记录
      this.data.typeClass = "奖励";
      this.data.url = "plugin.task-package.frontend.daily.reward-record";
      this.data.otherJson = {
        record_id: this.data.id
      };
    } else if (options.type == 2) {
      this.data.typeClass = "总贡献值";
      this.data.url = "plugin.task-package.frontend.active-value.get-list";
    } else if (options.type == 3) {
      this.data.typeClass = "贡献值";
      this.data.url = "plugin.task-package.frontend.record.contribution-value";
    } else if (options.type == 4) {
      this.data.typeClass = "复活";
      this.data.url = "plugin.task-package.frontend.recovery.recovery-list";
    } else if (options.type == 5) {
      this.data.typeClass = "手续费";
      this.data.url = "plugin.task-package.frontend.record.procedures-fee";
    } else if (options.type == 6) {
      this.data.typeClass = "平台流水";
      this.data.url = "plugin.task-package.frontend.record.platform-flow";
    } else if (options.type == 7) {
      this.data.typeClass = "下级团队";
      this.data.url = "plugin.task-package.frontend.index.sub-team";
    } else if (options.type == 8) {
      this.data.typeClass = "兑换";
      this.data.url = "plugin.task-package.frontend.record.get-list";
    }
    wx.setNavigationBarTitle({
      title: `${this.data.typeClass}记录`,
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.isLoadMore){
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  init() {
    this.setData({
      page: 1,
      isLoadMore: true,
      total_page: 0
    })
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    console.log(this.data.page,this.data.total_page)
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      this.setData({
        page: this.data.page
      })
      let json = {
        page: this.data.page,
        ...this.data.otherJson
      };

      app._getNetWork({
        url: app.getNetAddresss(this.data.url),
        data: json,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            let myData = [];
            if (this.data.type == 4) {
              myData = res.data.data;
              this.data.total_page = res.data.last_page;
              this.data.rankingList.data = [...this.data.rankingList.data, ...myData]; //数组拼接
            } else if (this.data.type == 1 || this.data.type == 8) {
              myData = res.data.data;
              this.data.total_page = res.data.last_page;
              this.data.rankingList = [...this.data.rankingList, ...myData]; //数组拼接
            } else {
              myData = res.data.list.data;
              this.data.total_page = res.data.list.last_page;
              this.data.rankingList.data = [...this.data.rankingList.data, ...myData]; //数组拼接
            }
            this.setData({
              total_page: this.data.total_page,
              rankingList: this.data.rankingList
            })
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  getData() {
    let json = {
      page: this.data.page,
      ...this.data.otherJson
    };
    app._getNetWork({
      url: app.getNetAddresss(this.data.url),
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res)
          if (this.data.type == 4) {
            this.data.rankingList = res.data;
            this.data.total_page = res.data.last_page;
          } else if (this.data.type == 1 || this.data.type == 8) {
            this.data.rankingList = res.data.data;
            this.data.total_page = res.data.last_page;
          } else {
            this.data.rankingList = res.data.list;
            this.data.total_page = res.data.last_page;
          }
          if (this.data.type == 7) {
            this.data.sub_team_count = res.data.sub_team_count;
          } else if (this.data.type == 2) {
            this.data.sub_team_count = res.data.total_amount;
          } else if (this.data.type == 3) {
            this.data.sub_team_count = res.data.total;
          } else if (this.data.type == 5 || this.data.type == 6) {
            this.data.sub_team_count = res.data.total;
            if (this.data.type == 6) {
              this.data.show = res.data.show;
              this.setData({
                usable_name:res.data.usable_name
              })
            }
          }else if(this.data.type == 8){
            this.setData({
              usable_name:res.data.usable_name
            })
          }
          this.setData({
            rankingList: this.data.rankingList,
            sub_team_count: this.data.sub_team_count,
            show: this.data.show,
            total_page:this.data.total_page
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
})